import com.alibaba.fastjson.JSONObject;
import com.lowagie.text.pdf.BaseFont;
import org.apache.commons.collections.map.HashedMap;
import org.xhtmlrenderer.pdf.ITextRenderer;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;

/**
 * Created by suweiming on 2017/12/20.
 */

public class ToPDF {
    public static void main(String[] args) throws Exception {

        ToPDF app = new ToPDF();
//        String path = "http%3A%2F%2Fwww.baidu.com%2F";
//        System.out.println(java.net.URLDecoder.decode(path, "UTF-8"));
//        path = java.net.URLDecoder.decode(path, "UTF-8");

//        System.out.println(app.toMap(path));
//        Object res = 1;
//        if (("1" instanceof String) || (res.equals(new Integer(1)))) {
//            System.out.println("====");
//        }
//        System.out.println(uri.getQuery().split("&")[0]);
        app.createPdf();
//        Long l  = System.currentTimeMillis()+1000*60*60*24*3;
//        System.out.println(new Date(l));
//        capText = capText.substring(capText.length()-6);
//        System.out.println(capText);

        Double d = 3.0;
        Double d2 = 3.00;

//        System.out.println(0.2*33);
//        System.out.println(Arith.div(1, 90, 3));
//        System.out.println(Arith.div(0.67, 90, 3));
//        BigDecimal bd1 = Arith.div(89, 90, 3);
//        BigDecimal bd2 = Arith.div(1, 90, 3);
//        System.out.println(bd1.add(bd2));
//        System.out.println(Arith.div(5000, 6550,3));
        //注意需要使用BigDecimal(String val)构造方法
//        BigDecimal bigDecimal = new BigDecimal(d );
//        BigDecimal bigDecimal2 = new BigDecimal((d2));
//        System.out.println(bigDecimal.equals(bigDecimal2));
//        long t = new Long("3000000000000000000");
//        long t = new Long("9223372036854775808");
//        System.out.println(Arith.mul(17.04, 5390));
//        System.out.println(Class.forName("ToPDF"));
//        System.out.println(Long.MAX_VALUE);
//        System.out.println(new BigDecimal("92233720368547758089223372036854775808"));

//        System.
    }

    public void createPdf() throws Exception {
        // step 1
        String inputFile = "index.html";
        String url = new File(inputFile).toURI().toURL().toString();
        String outputFile = "index.pdf";
        System.out.println(url);
        // step 2
        OutputStream os = new FileOutputStream(outputFile);
        org.xhtmlrenderer.pdf.ITextRenderer renderer = new ITextRenderer();
        renderer.setDocument(url);

        // step 3 解决中文支持
        org.xhtmlrenderer.pdf.ITextFontResolver fontResolver = renderer.getFontResolver();
//        fontResolver.addFont("c:/Windows/Fonts/simhei.ttf", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);

        fontResolver.addFont("C:/Windows/Fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        fontResolver.addFont("C:/Windows/Fonts/simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        fontResolver.addFont("C:/Windows/Fonts/simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

        renderer.layout();
        renderer.createPDF(os);
        os.close();
        String str = "由1变为0";
//        str.e
        System.out.println("create pdf done!!");
    }

    public Map<String, Object> toMap(String path) throws UnsupportedEncodingException, URISyntaxException {
//        String path = "http%3A%2F%2Flocalhost%2Fdeal.html%3Fid%3D14%26no%3D456789";

        path = java.net.URLDecoder.decode(path, "UTF-8");
        URI uri = new URI(path);
        Map<String, Object> map = new HashedMap();
        String[] str = uri.getQuery().split("&");
        for (String st : str) {
            String[] smap = st.split("=");
            map.put(smap[0], smap[1]);
        }
        System.out.println(map.toString());
        JSONObject jsonObject = JSONObject.parseObject(map.toString());
        return map;
    }
}
